// pages/smartlife/wangcai/transfer/transfer.js
var util = require('../../../../utils/util.js');
var common = require('../../../../utils/common.js');
//获取应用实例
var app = getApp()
Page({
  data: {
    typeList:[],
    memberArray: [],
    memberIndex: 0,
    outAccount:"",
    paternArray: [],
    paternIndex: 0,
    date: "",
    money:"",
    remark:"",
    btnFinishLoading:false,
    btnAgainLoading:false,
    finishText:"转账",
    againText:"再转一笔",
    moneyFocus:true
  },
  onLoad:function(options){
    var that = this;
    wx.setNavigationBarTitle({
      title: options.outaccount
    })
    that.setData({
      outAccount:options.outaccount,
      date: util.formatDate(new Date)
    })

    if(app.globalData.recordParam){
      // console.log(app.globalData.recordParam.patern.length)
      var inAccountArray = app.globalData.recordParam.patern.concat();
      inAccountArray.splice(0,1);
      that.setData({
        paternArray:inAccountArray
      })
      if(app.globalData.recordParam.member.length > 0){
        that.setData({
          memberArray:app.globalData.recordParam.member
        })
      }else{
        that.setData({
          memberArray:[{"member":"本人"}]
        })
      }
    }else{
      app.getRecordParam(function(paramRes){
        if(paramRes.status==1){
          // console.log(paramRes.data.patern.length)
          var inAccountArray = paramRes.data.patern.concat();
          inAccountArray.splice(0,1);
          that.setData({
            paternArray:inAccountArray
          })
          if(paramRes.data.member.length > 0){
            that.setData({
              memberArray:paramRes.data.member
            })
          }else{
            that.setData({
              memberArray:[{"member":"本人"}]
            })
          }
        }else{
          app.errorToGetToken()
        }
      })
    }

    
  },
  memberPickerChange: function(e) {
    this.setData({
      memberIndex: e.detail.value
    })
  },
  paternPickerChange: function(e) {
    this.setData({
      paternIndex: e.detail.value
    })
  },
  bindDateChange: function(e) {
    this.setData({
      date: e.detail.value
    })
  },
  btnComplete: function(e) {
    var that = this
    // 设置按钮加载状态
    this.setData({
      btnFinishLoading:true,
      finishText:"正在转账"
    })
    that.submitData(function(res){
        //更新按钮状态
        that.setData({
          btnFinishLoading:false,
          finishText:"转账"
        })
      if(res.status==-1){
      }else{
        //刷新上一页数据
        var pages = getCurrentPages();
        if(pages.length > 1){
            //上一个页面实例对象
            var prePage = pages[pages.length - 2];
            prePage.getData()
        }
        wx.navigateBack({
          delta: 1
        })
      }
    })
      
  },
  btnAddAgain: function(e) {
    var that = this
    //设置按钮加载状态
    this.setData({
      btnAgainLoading:true,
      againText:"正在转账"
    })
    this.submitData(function(res){
        //更新按钮状态
        that.setData({
          btnAgainLoading:false,
          againText:"再转一笔"
        })
      if(res.status==-1){
      }else{
        //刷新上一页数据
        var pages = getCurrentPages();
        if(pages.length > 1){
            //上一个页面实例对象
            var prePage = pages[pages.length - 2];
            prePage.getData()
        }
        //清除数据
        that.setData({
          money:"",
          remark:""
        })
      }
    })
  },
  submitData: function(cb) {
    var that = this;
    var money = that.data.money
    var member = that.data.memberArray[that.data.memberIndex].member
    var outaccount = that.data.outAccount
    var inaccount = that.data.paternArray[that.data.paternIndex].cardtype
    var createdate = that.data.date
    var remark = that.data.remark

    var returnRes = null
    if(money=="") {
      wx.showModal({
        title: '错误提示',
        content: '请填写正确的金额',
        showCancel:false
      })
      returnRes = {
      "status":-1,
      "msg":"请填写正确的金额"
      }
      typeof cb == "function" && cb(returnRes)
      return
    }
    // console.log('--------'+member+'--------'+createdate+'--------'+outaccount+'--------'+inaccount+'--------'+money+'--------'+remark)
    wx.request({
      url: common.apiUrl()+'addTransfer',
      data: {
        token:app.globalData.wangcaiToken,
        member:member,
        createdate:createdate,
        outaccount:outaccount,
        inaccount:inaccount,
        money:money,
        remark:remark
      },
      method:'POST',
      header: {
      'content-type': 'application/x-www-form-urlencoded'
        },
      success: function(res){
        if(res.data.status==1){
          wx.showToast({
            title: '添加成功',
            icon: 'success',
            success: function(e){
              //设置全局变量
              typeof cb == "function" && cb(res.data)
            }
          })
        }else{
          app.errorToGetToken()
        }
      }
    })
  },
  moneyInput: function(e) {
    this.setData({
      money: e.detail.value
    })
  },
  remarkInput: function(e) {
    this.setData({
      remark: e.detail.value
    })
  }
})